import cv2import globimport mathimport matplotlib.image as mpimgimport matplotlib.pyplot as pltimport numpy as npimport refrom moviepy.editor import VideoFileClipfrom scipy.signal import gaussian%matplotlib inlineCALIBRATION_FILES = 'camera_cal/calibration*.jpg'FIND_LANE_LINE_SOBEL_KERNEL = 3FIND_LANE_LINE_S_CHANNEL_THRESHOLDS = (170, 255)FIND_LANE_LINE_MAGNITUDE_X_THRESHOLDS = (20, 255)FIND_LANE_LINE_MAGNITUDE_Y_THRESHOLDS = (30, 255)FIND_LANE_LINE_MAGNITUDE_X_Y_THRESHOLDS = (30, 255)FIND_LANE_LINE_ANGLE_THRESHOLDS = (0.7, 1.3)CROP_AREA = [(81, 716), (554, 459), (735, 456), (1213, 713)]WARP_SOURCE = [(267, 713), (589, 461), (707, 460), (1152, 712)]WARP_DESTINATION = [(300, 720), (300, 0), (900, 0), (900, 720)]CENTROID_WINDOW_WIDTH = 80CENTROID_WINDOW_HEIGHT = 80CENTROID_MARGIN = 50CENTROID_MIN_PIXEL_REQUIRED = 50Y_METERS_PER_PIXEL = 30.0 / 720X_METERS_PER_PIXEL = 3.7 / 700CALIBRATION_MATRIX = NoneCALIBRATION_DIST_COEFFICIENTS = NonePERPECTIVE_TRANSFORM_MATRIX = NoneINVERSE_PERPECTIVE_TRANSFORM_MATRIX = Nonedef calibrate_camera(p_image_files, p_number_corners): """ Calibray camera from calibration images """ # 2D points in image plan image_points = [] # 3D points in real world space object_points = [] # Prepare object points like (0, 0, 0), (1, 0, 0), (2, 0, 0), ..., (7, 5, 0) for image_file in p_image_files: (nx, ny) = p_number_corners[int(re.findall('\d+', image_file)[0]) - 1] objp = np.zeros((nx * ny, 3), np.float32) objp[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2) image = cv2.imread(image_file) grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, corners = cv2.findChessboardCorners(grayscale_image, (nx, ny), None) if ret: image_points.append(corners) object_points.append(objp) cv2.drawChessboardCorners(image, (nx, ny), corners, ret) plt.figure() plt.title(image_file) plt.imshow(image) else: print('Unlable to find chess board corner of %s with nx as %d and ny as %d' % (image_file, nx, ny)) ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points, grayscale_image.shape[ : : -1], None, None) assert ret, 'Unable to calibrate the camer' global CALIBRATION_MATRIX, CALIBRATION_DIST_COEFFICIENTS CALIBRATION_MATRIX, CALIBRATION_DIST_COEFFICIENTS = mtx, dist# Calibrate camera using the calibrate imagescalibration_image_files = glob.glob(CALIBRATION_FILES)number_calibration_corners = [ \ (9, 5), (9, 6), (9, 6), (9, 6), (9, 5), \ (9, 6), (9, 6), (9, 6), (9, 6), (9, 6), \ (9, 6), (9, 6), (9, 6), (9, 6), (9, 6), \ (9, 6), (9, 6), (9, 6), (9, 6), (9, 6)]calibrate_camera(calibration_image_files, number_calibration_corners)def undistort(p_image): return cv2.undistort(p_image, CALIBRATION_MATRIX, CALIBRATION_DIST_COEFFICIENTS, None, CALIBRATION_MATRIX)# Apply calibration maxtrix to undistort calibration imagescalibration_image_files = glob.glob(CALIBRATION_FILES)for calibration_image_file in calibration_image_files: calibration_image = cv2.imread(calibration_image_file) undistorted_calibration_image = undistort(calibration_image) plt.figure() f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9)) f.tight_layout() ax1.imshow(calibration_image) ax1.set_title('Original %s' % calibration_image_file, fontsize=30) ax2.imshow(undistorted_calibration_image) ax2.set_title('Undistorted Image', fontsize=30) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)